package newcrunch.util;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

/**
 *
 * @author WL
 */
public class DisplayHelper extends javax.swing.JFrame 
{
    private DisplayHelper() {}
    
    public static void showErrorFrame(Component parent, String title, String message)
    {
        JOptionPane.showMessageDialog(parent, message, title, JOptionPane.ERROR_MESSAGE);
    }
    
    public static int showConfirmFrame(Component parent, String title, String message)
    {
        return JOptionPane.showConfirmDialog(parent, message, title, JOptionPane.OK_CANCEL_OPTION);
    }
    
    public static void showInfoFrame(Component parent, String title, String message)
    {
        JOptionPane.showMessageDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE);
    }

    public static JDialog getProgressFrame(Component owner, String title,
                                            String message,
                                            boolean indeterminate)
    {
        JDialog progressFrame = new javax.swing.JDialog();
        progressFrame.setTitle(title);

        JPanel basePanel = new javax.swing.JPanel();
        JLabel displayText = new javax.swing.JLabel();
        JProgressBar jProgressBar1 = new javax.swing.JProgressBar();

        displayText.setText(message); 

        jProgressBar1.setIndeterminate(indeterminate);

        javax.swing.GroupLayout basePanelLayout = new javax.swing.GroupLayout(basePanel);
        basePanel.setLayout(basePanelLayout);
        basePanelLayout.setHorizontalGroup(
            basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(basePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(basePanelLayout.createSequentialGroup()
                        .addComponent(displayText)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );

        basePanelLayout.setVerticalGroup(
            basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(basePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(displayText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout progressFrameLayout = new javax.swing.GroupLayout(progressFrame.getContentPane());
        progressFrame.getContentPane().setLayout(progressFrameLayout);
        progressFrameLayout.setHorizontalGroup(
            progressFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(basePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        progressFrameLayout.setVerticalGroup(
            progressFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(basePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        progressFrame.pack();
        progressFrame.setResizable(false);
        
        return progressFrame;
    }
    
    /**
     * Makes the frame appear in the center directly above the parent.
     * @param frame
     * @param parent 
     */
    public static void centralizePosition(JDialog frame, Component parent)
    {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            
        //centers the dialog
        Point point = parent.getLocationOnScreen();
        int x = point.x  + parent.getWidth() / 2 - frame.getWidth() / 2;
        int y = point.y + parent.getHeight() / 2 - frame.getHeight() / 2;

        x = x >= dim.getWidth() ? x - frame.getWidth() : x;
        y = y >= dim.getHeight() ? y - frame.getHeight() : y;

        frame.setBounds(x, y, frame.getWidth(), frame.getHeight());
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressFrame = new javax.swing.JDialog();
        basePanel = new javax.swing.JPanel();
        displayText = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();

        progressFrame.setTitle("Processing Source Code...");

        displayText.setText("Processing Source Code...");

        jProgressBar1.setIndeterminate(true);

        javax.swing.GroupLayout basePanelLayout = new javax.swing.GroupLayout(basePanel);
        basePanel.setLayout(basePanelLayout);
        basePanelLayout.setHorizontalGroup(
            basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(basePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(basePanelLayout.createSequentialGroup()
                        .addComponent(displayText)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        basePanelLayout.setVerticalGroup(
            basePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(basePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(displayText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout progressFrameLayout = new javax.swing.GroupLayout(progressFrame.getContentPane());
        progressFrame.getContentPane().setLayout(progressFrameLayout);
        progressFrameLayout.setHorizontalGroup(
            progressFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(basePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        progressFrameLayout.setVerticalGroup(
            progressFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(basePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    /**
     * Makes the current line where the caret is at be at the centre of the 
     * scrollpane.
     * 
     * Credits to Rob Camick at 
     * http://tips4java.wordpress.com/2009/01/04/center-line-in-scroll-pane/
     * @param component 
     */
    public static void centerLineInScrollPane(JTextComponent component)
    {
            Container container = SwingUtilities.getAncestorOfClass(JViewport.class, component);

            if (container == null) 
                return;

            try
            {
                    Rectangle r = component.modelToView(component.getCaretPosition());
                    JViewport viewport = (JViewport)container;
                    int extentHeight = viewport.getExtentSize().height;
                    int viewHeight = viewport.getViewSize().height;

                    //prevent scrolling from going above line 1
                    int y = Math.max(0, r.y - (extentHeight / 2));
                    //prevent scrolling from going below the last line
                    y = Math.min(y, viewHeight - extentHeight);
                    
                    //do the same for x coordinates
                    int extentWidth = viewport.getExtentSize().width;
                    int viewWidth = viewport.getViewSize().width;
                    
                    int x = Math.max(0, r.x - (extentWidth / 2));
                    x = Math.min(x, viewWidth - extentWidth);

                    viewport.setViewPosition(new Point(x, y));
            }
            catch(BadLocationException ble) {
                ble.printStackTrace();
            }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel basePanel;
    private javax.swing.JLabel displayText;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JDialog progressFrame;
    // End of variables declaration//GEN-END:variables
}
